package cn.itcast.controller;

import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**常用的注解
 * @author Honglixi
 * @create 2021-03-20 10:18
 */
@Controller
@RequestMapping("/anno")
public class AnnoController  {

    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name = "name") String username){
        System.out.println("执行了");
        System.out.println(username);
        return "success";
    }

    /**
     * 获取到请求体的内容
     * @param body
     * @return
     */
    @RequestMapping(value = "/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println("执行了");
        System.out.println(body);
        return "success";
    }

    /**
     * PathVariable注解
     * @return
     */
    @RequestMapping(value = "/testPathVariable/{sid}")
    public String testPathVariable(@PathVariable(name = "sid")String id){
        System.out.println("执行了");
        System.out.println(id);
        return "success";
    }

    /**
     * ModelAttribute注解
     * @return
     */
    @RequestMapping(value = "/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user){
        System.out.println("testModelAttribute执行了");
        System.out.println(user);
        return "success";
    }
    //方法二 @ModelAttribute 注解先执行
    @ModelAttribute
    public void showUser(String uname, Map<String,User> map){
        System.out.println("showUser执行了");
        User user =new User();
        user.setUname(uname);
        user.setAge(20);
        user.setDate(new Date());
        map.put("abc",user);
    }
    /**
     * 该方法会先执行
     */
    //方法一
//    @ModelAttribute
//    public User showUser(String uname){
//        System.out.println("showUser执行了。。。");
//        //通过用户查询数据库
//        User user = new User();
//        user.setUname(uname);
//        user.setAge(18);
//        user.setDate(new Date());
//        return user;


}



